﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ALinq.BBS.DataAccess.Properties;

namespace ALinq.BBS.DataAccess
{
    public static class DataUtility
    {
        public static void SetDatabasePath(string path)
        {
            Settings.Default["BBSConnectionString"] = path;
        }

        public static void CreateDatabaseIfNotExists()
        {
            var db = new Database();
            if (db.DatabaseExists() == false)
                db.CreateDatabase();
        }

        public static void SetReplyListSize(int value)
        {
            Settings.Default["SetReplyListSize"] = value;
        }

        public static void SetPostListSize(int value)
        {
            Settings.Default["SetPostListSize"] = value;
        }

#if DEBUG
        /// <summary>
        /// 用来修复数据库中的冗余数据。
        /// </summary>
        public static void FixDatabase()
        {
            var db = new Database();
            foreach (var user in db.Users)
            {
                var postCount = db.Posts.Count(o => o.UserName == user.Name);
                db.Users.Update(o => new User { PostsCount = postCount },
                                o => o.ID == user.ID);
            }
        }
#endif
    }
}
